package com.cos.shop.web.controller.pay;

import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.cos.shop.common.AlipayConfig;
import com.cos.shop.common.Const;
import com.cos.shop.common.ResponseResult;
import com.cos.shop.services.OrderServices;
import com.cos.shop.services.impl.OrderServicesImpl;
import com.cos.shop.vo.OrderDetailVo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @author: COS
 * @time: 2023/5/12 15:04
 * @description:
 */
@WebServlet("/getReturn")
public class PayReturnServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取支付宝GET过来反馈信息
        Map<String, String> params = new HashMap<String, String>();
        Map<String, String[]> requestParams = request.getParameterMap();
        for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext(); ) {
            String name = (String) iter.next();
            String[] values = (String[]) requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i]
                        : valueStr + values[i] + ",";
            }
            //乱码解决，这段代码在出现乱码时使用
            valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
            params.put(name, valueStr);
        }
        try {
            boolean signVerified = AlipaySignature.rsaCheckV1(params, AlipayConfig.alipay_public_key, AlipayConfig.charset, AlipayConfig.sign_type); //调用SDK验证签名
            String msg = "支付失败";
            //——请在这里编写您的程序（以下代码仅作参考）——
            if (signVerified) {
                //商户订单号
                String out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"), "UTF-8");

                //支付宝交易号
                String trade_no = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"), "UTF-8");

                //付款金额
                String total_amount = new String(request.getParameter("total_amount").getBytes("ISO-8859-1"), "UTF-8");
                OrderServices os = new OrderServicesImpl();
                System.out.println(out_trade_no);
                OrderDetailVo orderDetail = os.getOrderDetail(out_trade_no);
                System.out.println(1);
                System.out.println(orderDetail);
                if (orderDetail != null) {
                    double ocount = Double.valueOf(total_amount);
                    System.out.println(2+":total_amount:"+total_amount);
                    if (orderDetail.getOcount()==ocount) {
                        msg = "支付成功,单号:" + out_trade_no + ",金额:" + total_amount;
                    }
                }
            }
            request.setAttribute(Const.MSG, ResponseResult.createSuccess(msg));
            request.getRequestDispatcher("message.jsp").forward(request,response);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
    }
}
